//
//  BRSchemeManage.m
//  BrezzeSaaS
//
//  Created by 甘卫国 on 2024/3/5.
//  Copyright © 2024 Brezze GWG. All rights reserved.
//

#import "BRSchemeManage.h"

@implementation BRSchemeManage

//获取其中某个参数
+ (NSString *)br_getParamByName:(NSString *)name URLString:(NSString *)url {
    if (url.length == 0) return @"";
    NSError *error;
    NSString *regTags=[[NSString alloc] initWithFormat:@"(^|&|\\?)+%@=+([^&]*)(&|$)", name];
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regTags
                                                                           options:NSRegularExpressionCaseInsensitive
                                                                             error:&error];
    
    // 执行匹配的过程
    NSArray *matches = [regex matchesInString:url
                                      options:0
                                        range:NSMakeRange(0, [url length])];
    for (NSTextCheckingResult *match in matches) {
        NSString *tagValue = [url substringWithRange:[match rangeAtIndex:2]];  // 分组2所对应的串
        return tagValue;
    }
    return @"";
}

+ (NSDictionary *)br_urlDic:(NSString *)urlStr {
    
    if ([urlStr containsString:@"/#"]) {
        urlStr = [urlStr stringByReplacingOccurrencesOfString:@"/#" withString:@""];
    }
    
//    if ([urlStr containsString:@"\\u"]) {
//        urlStr = [BRSchemeManage decodeUnicodeString:urlStr];
//    }
    NSURL *url = [NSURL URLWithString:urlStr];
    if (!url) return @{};
    NSURLComponents *urlComponents = [NSURLComponents componentsWithURL:url resolvingAgainstBaseURL:NO];
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    for (NSURLQueryItem *queryItem in urlComponents.queryItems) {
        if (queryItem.value) {
            [params setObject:queryItem.value forKey:queryItem.name];
        }
    }
    
    NSLog(@"%@", params);
    return params;
}

+ (NSString *)decodeUnicodeString:(NSString *)unicodeString {
    NSMutableString *resultString = [NSMutableString string];
    NSScanner *scanner = [NSScanner scannerWithString:unicodeString];
    
    while (![scanner isAtEnd]) {
        NSString *substring = nil;
        
        // 找到下一个 \u 开始的位置
        [scanner scanUpToString:@"\\u" intoString:&substring];
        if (substring) {
            [resultString appendString:substring];
        }
        
        // 如果还有 \u 开头的序列，则解析之
        if (![scanner isAtEnd]) {
            // 扫描 \u 后面的四个十六进制数字
            NSString *unicodeValue = nil;
            [scanner scanString:@"\\u" intoString:nil];
            [scanner scanUpToString:@"" intoString:&unicodeValue];
            
            if (unicodeValue) {
                // 将 Unicode 转义序列解析为字符
                NSScanner *unicodeScanner = [NSScanner scannerWithString:unicodeValue];
                unsigned int unicodeInt = 0;
                [unicodeScanner scanHexInt:&unicodeInt];
                [resultString appendFormat:@"%C", (unichar)unicodeInt];
            }
        }
    }
    
    return resultString;
}


@end

@implementation BRSchemeModel



@end
